<?php
namespace Admin\Controller;
use Think\Controller;

class CodeController extends Controller {

    public function index(){
//        dump($_GET);
        $co_id = $_GET['co_id'];

        $code = D("Code");
        $info = $code->find($co_id);
        $paths = $info['co_path'].$info['co_name'];
        //中间处理文件路径
        $newpaths = './resource/cache.html';
//        echo $paths;
        copy($paths,$newpaths);

        $this->assign("co_id",$co_id);
        $this->display();
    }

    public function show(){

        $code = D("Code");

        $count = $code->count();//获取所有数据可以中间加where控制条件
        $page = new \Think\Page($count,6);//条数和每页个数
        $page->rollPage = 4;//控制显示页数
        $page->setConfig('last', '末页');
        $page->setConfig('first', '首页');

        $pages = $page->show();
        $infos = $code->order('co_id')->limit($page->firstRow.','.$page->listRows)->select();

        $this->assign('page',$pages);// 赋值分页输出
        $this->assign("infos",$infos);
        $this->display();
    }

    public  function newbuild(){

        $code = D("Code");
        $co_space = $_POST["co_space"];
        $co_name = $_POST["co_name"];

        $info['co_space'] = $co_space;
        $info['co_type'] = 'php';


        //空间是否为空
        if(!empty($co_space)){
            $info['co_path'] = "./resource/".$co_space."/";
            if(!empty($co_name)){
                if(strpos($co_name,'.php')){
                    //验证文件是否已存在
                    $x = $code->where("co_space = $co_space and co_name = '$co_name'")->select();
                    if(empty($x)){
                        $info['co_name'] = $co_name;
                        $z = $code->add($info);

                        $path = $info['co_path'].$co_name;
//                        echo $path;
                        //只会建文件，不会建文件夹(路径)
                        file_put_contents($path,"");

                        if($z){ $error = "创建成功!";}
                    }else{
                        $error = "文件已存在！";
                    }

                }else{
                    $error = "不是(.php)后缀！";
                }
            }else{
                $error = "请填写文件名！";
            }
        }else{
            $error = "请选择空间！";
        }
        $sql = "select co_space from cl_code group by co_space;";
        $space = $code->query($sql);

        $this->assign("error",$error);
        $this->assign("space",$space);
        $this->display();
    }

    public function del($co_id){

        $code = D('Code');
        $z = $code->delete($co_id);

        if($z){
            //删除html中的缓存数据
            $this->redirect('show',array(),1,'删除成功');
        }else{
            $this->redirect('show',array('co_id'=>$co_id),1,'删除失败');
        }

    }

    public function ajax_save(){

        //把html预定义字符转化为字符
        $co_id = htmlspecialchars_decode($_POST['co_id'],ENT_QUOTES);
        $info_dm =  htmlspecialchars_decode($_POST['php'],ENT_QUOTES);

        $code = new \Model\CodeModel();
        $info = $code->find($co_id);
        $co_name = $info['co_name'];
        $path = $info['co_path'];

//        $code->timesave($co_id);
        //判断文件是否存在
        if(!is_dir($path)){
            mkdir($path,0777);
        }

        if(!empty($info_dm)){
            //只会建文件，不会建文件夹(路径)
            $date = file_put_contents($path.$co_name,$info_dm);
        }
        if($date){
            $code->timesave($co_id);
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(2);
        }

    }


}